<?php

namespace App\Http\Controllers\Admin\AirLine;

use App\Http\Controllers\Controller;
use App\Models\DestinationRuleModel;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;

//客户排货偏好
class DestinationRule extends Controller
{

    public function list(Request $request) {
        $page = $request->input('page', 1);
        $limit = $request->input('limit', 20);
        $country_id = $request->input('destination', '');
        $port_id = $request->input('port_id', '');
        $create_start = $request->input('startTime', '');
        $create_end = $request->input('endTime', '');

        $query = DestinationRuleModel::query()
            ->with([
                'create_admin',
                'country',
                'port'
            ]);

        if($country_id){
            $query->where('country_id', $country_id);
        }
        if($port_id){
            $query->where('port_id', $port_id);
        }

        if($create_start && $create_end){
            $query->whereBetween('created_at', [$create_start, $create_end]);
        }

        $total = $query->count('id');
        $items = $query
            ->offset(($page-1) * $limit)
            ->limit($limit)
            ->orderBy('id', 'desc')
            ->get();

        $list = $items->map(function($item) {
            return [
                'id' => $item->id,
                'country_id' => $item->country_id,
                'country_name' => $item->country ? $item->country->name : '',
                'port_id' => $item->port_id,
                'port_name' => $item->port ? $item->port->name : '',
                'first' => $item->first,
                'priority' => implode('>', [
                    DestinationRuleModel::RULE_MAPS[$item->first] ?? '',
                    DestinationRuleModel::RULE_MAPS[$item->second] ?? '',
                    DestinationRuleModel::RULE_MAPS[$item->third] ?? ''
                ]),
                'second' => $item->second,
                'third' => $item->third,
                'created_at' => $item->created_at->toDateTimeString(),
                'create_user' => $item->create_admin->staff->name ?? '',
            ];
        });

        return $this->success(['list' => $list, 'total' => $total]);
    }

    public function add(Request $request){
        $auth = $request->get('auth');
        $params = $request->all();

        $validator = Validator::make($params, [
            'country_id' => 'required|integer',
            'port_id' => 'required|integer',
            'first' => 'required|integer|in:1,2,3',
            'second' => 'required|integer|in:1,2,3',
            'third' => 'required|integer|in:1,2,3',
        ], [
            'country_id.required' => '请选择目的地国家',
            'port_id.required' => '请选择目的地口岸',
            'first.required' => '请选择第一优先级',
            'second.required' => '请选择第二优先级',
            'third.required' => '请选择第三优先级',
        ]);

        if($validator->fails()) {
            return $this->error($validator->errors()->first());
        }

        if(count(
            array_unique([$params['first'], $params['second'], $params['third']])
            ) != 3) {
            return $this->error('优先级不能重复');
        }

        $rule = new DestinationRuleModel();
        $rule->country_id = $params['country_id'];
        $rule->port_id = $params['port_id'];
        $rule->first = $params['first'];
        $rule->second = $params['second'];
        $rule->third = $params['third'];
        $rule->created_admin_id = $auth->id;
        $rule->save();
        return $this->success();
    }

    public function update(Request $request, $id){
        $auth = $request->get('auth');
        $params = $request->all();

        $validator = Validator::make($params, [
            'country_id' => 'required|integer',
            'port_id' => 'required|integer',
            'first' => 'required|integer|in:1,2,3',
            'second' => 'required|integer|in:1,2,3',
            'third' => 'required|integer|in:1,2,3',
        ], [
            'country_id.required' => '请选择目的地国家',
            'port_id.required' => '请选择目的地口岸',
            'first.required' => '请选择第一优先级',
            'second.required' => '请选择第二优先级',
            'third.required' => '请选择第三优先级',
        ]);

        if($validator->fails()) {
            return $this->error($validator->errors()->first());
        }

        if(count(
            array_unique([$params['first'], $params['second'], $params['third']])
            ) != 3) {
            return $this->error('优先级不能重复');
        }

        $rule = DestinationRuleModel::find($id);
        if(!$rule) {
            return $this->error('规则不存在');
        }
        $rule->country_id = $params['country_id'];
        $rule->port_id = $params['port_id'];
        $rule->first = $params['first'];
        $rule->second = $params['second'];
        $rule->third = $params['third'];
        $rule->save();
        return $this->success();
    }

    public function delete(int $id){
        DestinationRuleModel::destroy($id);
        return $this->success();
    }

}